package mapper

import (
	"im/global"
	"im/model"
	"im/model/request"
)

type GroupMemberMapper struct{}

// 群添加成员
func AddMember(gmr request.ChatGroupMemberReq) (model.GroupMember, error) {
	var chatGroup = model.GroupMember{
		GroupId: gmr.GroupId,
		UserId:  gmr.UserId,
	}
	return chatGroup, global.DB.Create(&chatGroup).Error
}

// 群删除成员
func DelMember(gmr request.ChatGroupMemberReq) (model.GroupMember, error) {
	var chatGroup = model.GroupMember{
		GroupId: gmr.GroupId,
		UserId:  gmr.UserId,
	}
	d := global.DB.Model(&model.GroupMember{})
	if groupId := chatGroup.GroupId; groupId != 0 {
		d.Where("group_id = ?", groupId)
	}
	if userId := chatGroup.UserId; userId != 0 {
		d.Where("user_id = ?", userId)
	}

	return chatGroup, d.Delete(&chatGroup).Error
}

// 群成员列表
func ListMember(gmr request.ChatGroupMemberReq) ([]model.GroupMember, error) {
	var chatGroup = model.GroupMember{
		GroupId: gmr.GroupId,
	}

	var groupMembers []model.GroupMember
	return groupMembers,
		global.DB.Model(&model.GroupMember{}).Preload("ChatGroup").Preload("User").Where("group_id = ?", chatGroup.GroupId).Find(&groupMembers).Error
}
